/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vanniktech.onabilityresult.sample.slice;

import com.onabilityresult.AbilityResult;
import com.onabilityresult.IntentData;
import com.onabilityresult.OnAbilityResult;
import com.vanniktech.onabilityresult.sample.ForResultAbility;
import com.vanniktech.onabilityresult.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

import java.util.Locale;

/**
 * 主页实例
 *
 * @since 2021-05-07
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    /**
     * 结果返回码-取消
     */
    public static final int RESULT_CANCELED = 0;

    /**
     * 结果返回码-完成
     */
    public static final int RESULT_OK = -1;

    /**
     * 结果返回码-自定义
     */
    public static final int RESULT_USER_DEFINED = 2;
    private static final int REQUEST_CODE_100 = 100;
    private static final int REQUEST_CODE_200 = 200;
    private static final int REQUEST_CODE_300 = 300;

    /**
     * onStart
     *
     * @param intent 意图
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button btnSkip1 = (Button) findComponentById(ResourceTable.Id_btn_skip1);
        Button btnSkip2 = (Button) findComponentById(ResourceTable.Id_btn_skip2);
        Button btnSkip3 = (Button) findComponentById(ResourceTable.Id_btn_skip3);

        btnSkip1.setText(getString(ResourceTable.String_mainBtnText1).toUpperCase(Locale.ROOT));
        btnSkip2.setText(getString(ResourceTable.String_mainBtnText2).toUpperCase(Locale.ROOT));
        btnSkip3.setText(getString(ResourceTable.String_mainBtnText3).toUpperCase(Locale.ROOT));

        btnSkip1.setClickedListener(this);
        btnSkip2.setClickedListener(this);
        btnSkip3.setClickedListener(this);
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent 意图
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 点击事件
     *
     * @param component 组件
     */
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn_skip1:
                skipPage(REQUEST_CODE_100);
                break;
            case ResourceTable.Id_btn_skip2:
                skipPage(REQUEST_CODE_200);
                break;
            case ResourceTable.Id_btn_skip3:
                // 跳转系统相册
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withAction(IntentConstants.ACTION_CHOOSE)
                        .build();
                intent.setType("image/*");
                intent.setOperation(operation);
                startAbilityForResult(intent, REQUEST_CODE_300);
                break;
            default:
                break;
        }
    }

    /**
     * 跳转界面
     *
     * @param requestCode 请求码
     */
    private void skipPage(int requestCode) {
        Intent intent = new Intent();

        Operation operation = new Intent.OperationBuilder()
                .withBundleName(ForResultAbility.class.getPackage().getName())
                .withAbilityName(ForResultAbility.class.getName())
                .build();

        intent.setOperation(operation);
        startAbilityForResult(intent, requestCode);
    }

    /**
     * 界面返回
     *
     * @param requestCode 请求码
     * @param resultCode 结构码
     * @param resultData 结构数据
     */
    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        AbilityResult.onResult(requestCode, resultCode, resultData).into(this);
    }

    /**
     * 显示Toast
     *
     * @param text 显示的文字
     */
    private void showToast(String text) {
        Component toastLayout = LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_layout_toast, null, false);
        Text toastText = (Text) toastLayout.findComponentById(ResourceTable.Id_text_msg_toast);
        toastText.setText(text);
        new ToastDialog(this)
                .setComponent(toastLayout)
                .setTransparent(true)
                .show();
    }

    /**
     * 界面返回-ok
     */
    @OnAbilityResult(requestCode = REQUEST_CODE_100, resultCodes = {RESULT_OK})
    void onAbilityResultOk() {
        showToast("Got ability for result ok");
    }

    /**
     * 界面返回-取消
     */
    @OnAbilityResult(requestCode = REQUEST_CODE_100, resultCodes = {RESULT_CANCELED})
    void onAbilityResultCanceled() {
        showToast("Got ability for result canceled");
    }

    /**
     * 界面返回-用户定义
     *
     * @param resultCode 请求码
     */
    @OnAbilityResult(requestCode = REQUEST_CODE_100, resultCodes = {RESULT_USER_DEFINED})
    void onAbilityResultUserDefined(final int resultCode) {
        showToast("Got ability for result user defined. resultCode = " + resultCode);
    }

    /**
     * 界面返回-有意图的
     *
     * @param resultCode 请求码
     * @param intent 意图
     */
    @OnAbilityResult(requestCode = REQUEST_CODE_200)
    void onAbilityResultCustom(final int resultCode, final Intent intent) {
        showToast("Got ability for result 2 " + resultCode + " with intent " + intent);
    }

    /**
     * 界面返回-从系统图库选择图片
     *
     * @param resultCode 请求码
     * @param intent 意图
     * @param uri Uri
     */
    @OnAbilityResult(requestCode = REQUEST_CODE_300)
    void onAbilityResultPickImage(final int resultCode, final Intent intent, @IntentData final Uri uri) {
        showToast("Got image for result " + resultCode + " with intent " + intent + " and uri " + uri);
    }
}
